//
//  NowEngineerController.m
//  PianoAndPoemDirectSeeding
//
//  Created by 梅津山 on 2017/7/21.
//  Copyright © 2017年 梅津山. All rights reserved.
//

#import "NowEngineerController.h"
#import "LeftController.h"
#import "EngineerController.h"

#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height

@interface NowEngineerController ()

@property (nonatomic, strong) EngineerController *home;
@property (nonatomic, strong) LeftController *left;
@property (nonatomic, strong) UIButton *btn;
@end

@implementation NowEngineerController

+ (instancetype)sharedNow {
    static NowEngineerController *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[NowEngineerController alloc] init];
    });
    return instance;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Engineer" bundle:nil];
    _home = [sb instantiateViewControllerWithIdentifier:@"EngineerController"];
    UIStoryboard *sb2 = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    _left = [sb2 instantiateViewControllerWithIdentifier:@"LeftController"];
    _left.view.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H);
    [self addChildViewController:_left];
    [self addChildViewController:_home];
    [self.view addSubview:_left.view];
    [self.view addSubview:_home.view];
    UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self.view addGestureRecognizer:pan];
}

- (void)handlePan:(UIPanGestureRecognizer*) recognizer {
    CGPoint translation = [recognizer translationInView:self.view];
    //增量后的x坐标位置
    CGFloat Xresult = translation.x + _home.view.bounds.origin.x;
    
    //向右
    if (translation.x >= 0) {
        if (_home.view.bounds.origin.x >= SCREEN_W / 4 * 3 || Xresult >= SCREEN_W / 4 * 3) {
            [self leftMynow];
            return;
        }
    } else if (translation.x < 0) {//向左
        //leftView已全部收回，则无法再向左
        if (_home.view.frame.origin.x <= 0 || Xresult <= 0) {
            _home.view.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H);
            [self.btn removeFromSuperview];
            return;
        }
    }
    
    CGRect frame = _home.view.frame;
    frame.origin.x += translation.x;
    _home.view.frame = frame;
    //清空移动的距离，这是关键
    [recognizer setTranslation:CGPointZero inView:recognizer.view];
    //做弹回效果，以中轴为界限
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        if (_home.view.frame.origin.x > SCREEN_W / 4) {
            [self closeView:NO];
        } else {
            [self closeView:YES];
        }
    }
}
- (void)closeView:(BOOL)close {
    if (close) {
        [self moveView:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
        [self.btn removeFromSuperview];
    } else {
        [self moveView:CGRectMake(SCREEN_W / 4 * 3, 0, SCREEN_W, SCREEN_H)];
        [self leftMynow];
    }
}

- (void)moveView:(CGRect)frame {
    [UIView animateWithDuration:0.3 animations:^{
        _home.view.frame = frame;
    } completion:^(BOOL finished) {
        
    }];
}


- (void)leftMynow{
    //    _left.view.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H);
    _home.view.frame = CGRectMake(SCREEN_W /4 * 3, 0, SCREEN_W, SCREEN_H);
    
    
    [self.view addSubview:self.btn];
    [self.btn addTarget:self action:@selector(leftBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view bringSubviewToFront:self.btn];
}
- (void)leftBtnClicked{
    NowEngineerController *now = [NowEngineerController sharedNow];
    [now tabbarMynow];
    [self.btn removeFromSuperview];
}

- (void)tabbarMynow{
    //    _left.view.frame = CGRectMake(-SCREEN_W /4 * 3, 0, SCREEN_W, SCREEN_H);
    _home.view.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H);
}

- (UIButton *)btn{
    if (!_btn) {
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn.frame = CGRectMake(SCREEN_W /4 * 3, 0, SCREEN_W / 4, SCREEN_H);
    }
    return _btn;
}

@end
